package cn.ctbu.simple_score_manage.common;


import javax.validation.constraints.NotNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 全局异常处理
 */
@ControllerAdvice(annotations = {RestController.class})
//表示返回json数据
@ResponseBody
@Slf4j
public class GlobalExceptionHandler {
    @ExceptionHandler({RunException.class})
    public R exceptionHandler(@NotNull RunException ex){
        log.error("出现异常,code为 "+ex.getCode()+"提示为: "+ex.getMessage());
        return R.error(ex);
    }
    /**
     * 进行异常处理
     * @return
     */
    //表示处理所有异常,后期会进行修改
    @ExceptionHandler(Exception.class)
    public R exceptionHandler(@NotNull Exception ex){
        log.error("出现异常"+ex.getMessage());
        return R.error(REnum.UNKNOWN_ERROR);
    }
}
